<template>
    <el-aside :width="$store.state.isCollapse ? '180px' : '64px'">
        <el-menu class="el-menu-vertical-demo" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b"
            :collapse="!$store.state.isCollapse" :collapse-transition="false">
            <h3 v-show="$store.state.isCollapse">后台管理</h3>
            <h3 v-show="!$store.state.isCollapse">后台</h3>
            <el-menu-item v-for="item in noChildren()" :index="item.path" :key="item.path" @click="clickMenu(item)">
                <component class="icons" :is="item.icon"></component>
                <span>{{ item.label }}</span>
            </el-menu-item>
            <el-sub-menu v-for="item in hasChildren()" :index="item.path" :key="item.path">
                <template #title>
                    <component class="icons" :is="item.icon"></component>
                    <span>{{ item.label }}</span>
                </template>
                <el-menu-item-group #title>
                    <el-menu-item v-for="(subItem, subIndex) in item.children" :key="subIndex" :index="subItem.path" @click="clickMenu(subItem)">
                        <component class="icons" :is="subItem.icon"></component>
                        <span>{{ subItem.label }}</span>
                    </el-menu-item>
                </el-menu-item-group>
            </el-sub-menu>
        </el-menu>
    </el-aside>
</template>

<script>
import { useRouter } from 'vue-router'
export default {
    setup() {
        const list = [
            {
                path: '/',
                name: 'home',
                label: '首页',
                icon: 'House',
                url: 'Home/Home'
            },
            {
                path: '/user',
                name: 'user',
                label: '用户管理',
                icon: 'user',
                url: 'UserManage/UserManage'
            },
            {
                label: '其他',
                icon: 'location',
                children: [
                    {
                        path: '/page1',
                        name: 'page1',
                        label: '页面1',
                        icon: 'setting',
                        url: 'Other/PageOne'
                    },
                    {
                        path: '/page2',
                        name: 'page2',
                        label: '页面2',
                        icon: 'setting',
                        url: 'Other/PageTwo'
                    }
                ]
            }
        ];
        const router = useRouter();
        const noChildren = () => {
            return list.filter((item) => !item.children)
        }
        const hasChildren = () => {
            return list.filter((item) => item.children)
        }
        const clickMenu = (item) => {
            router.push({
                name: item.name
            })
        }
        return {
            noChildren,
            hasChildren,
            clickMenu
        }
    }
}

</script>

<style lang="less" scoped>
.icons {
    width: 16px;
    height: 16px;
}

.el-menu {
    border-right: none;
}

.el-menu {
    height: 100%;
    border: none;

    h3 {
        color: #fff;
        text-align: center;
        line-height: 48px;
    }
}
</style>
